package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 获取某个问题的回答数量
     * @param id
     * @return
     */
    @Override
    public int getNumByQuestionId(Long id) {
        return answerMapper.selectNumByQuestionId(id);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        qo.setOrderBy("replyTime DESC");
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Answer> list=answerMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Answer answer) {
        User currentUser = UserContext.getCurrentUser();
        answer.setUser(currentUser);
        answer.setReplyTime(new Date());
        try {
            answerMapper.insert(answer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
